# -*- coding: utf-8 -*-
import os
import smtplib
import sys
from email.mime.text import MIMEText
from email.utils import formataddr


class SendEmail():

    def red_log(self):
        print(sys.path[0])
        current_dir = os.path.dirname(os.path.abspath(__file__))
        parent_dir = os.path.dirname(current_dir)
        init_data_dir = os.path.join(parent_dir, 'auto_res', 'res.log')
        log = open(init_data_dir, 'r')
        send_txt = ""
        for tmp in log.readlines():
            send_txt = send_txt + str(tmp)
        return send_txt

    def read_file_stream(self):
        with open("../old_auto_test/auto_res/res.log", 'rb') as f:
            f.seek(2)  # 偏置函数
            while True:
                if f:
                    f.read(2)
                else:
                    break

    def send_mail(self):
        send_txt = self.red_log()
        print(send_txt)
        my_sender = 'autotest@whaleops.com'
        my_pass = 'ND9qD3gQZ6HfcbTz'
        my_user = 'wenzixin@whaleops.com;autotest@whaleops.com'

        ret = True
        try:
            msg = MIMEText(send_txt, 'plain', 'utf-8')
            msg['From'] = formataddr(["测试组", my_sender])
            msg['To'] = formataddr(["WT研发", my_user])
            msg['Subject'] = "WT自动化测试结果"

            server = smtplib.SMTP_SSL("smtp.exmail.qq.com", 465)
            server.login(my_sender, my_pass)
            server.sendmail(my_sender, [my_user, ], msg.as_string())
            server.quit()
        except Exception as e:
            print(e)
            ret = False
        print(ret)
        return ret


if __name__ == '__main__':
    m = SendEmail()
    m.red_log()
    # m.red_log()
    # m.send_mail()
